import { Component, OnInit, forwardRef, ChangeDetectorRef, ChangeDetectionStrategy, Input, Output } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { DatePipe } from '@angular/common';
import { deepCopy } from 'src/app/util/util';
import { EventEmitter } from '@angular/core';

@Component({
  selector: 'min-date-picker',
  templateUrl: './date-picker.component.html',
  styleUrls: ['./date-picker.component.less'],
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => DatePickerComponent),
    multi: true
  }, DatePipe],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DatePickerComponent implements OnInit {
  public yearList: Array<number> = [];
  public monthList: Array<number> = [];
  public dayList: Array<number> = [];
  public currentDays: Array<any> = [];
  public currentYears: Array<any> = [];
  public currentMonths: Array<any> = [];

  public nowDate: Date = new Date();
  public nowYear: number = this.nowDate.getFullYear();
  public nowDay: number = this.nowDate.getDate();
  public nowMonth: number = this.nowDate.getMonth();
  public nowWeek: number = this.nowDate.getDay();
  public nowHour: any = this.nowDate.getHours();
  public nowMinute: any = this.nowDate.getMinutes();

  public moreMonth: Array<number> = [0, 2, 4, 6, 7, 9, 11];
  public weekList: Array<string> = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

  public isSelectDay: boolean = true;
  public isYear: boolean = true;
  public selectDate: string;
  public showType: string;
  public showMonth = this.nowMonth < 9 ? `0${this.nowMonth + 1}` : this.nowMonth + 1;

  @Input() type: string = "LLL dd, yyyy";
  @Output() typeChange = new EventEmitter();

  constructor(
    private DatePipe: DatePipe,
    private ref: ChangeDetectorRef
  ) {

  }
  onModelChange: Function = () => { };
  onModelTouched: Function = () => { };

  ngOnInit() {
    this.handleDate();
  }
  handleDate() {
    let month;
    this.yearList = [];
    this.currentDays = [];
    this.currentMonths = [];
    this.currentYears = [];
    for (let i = 2000; i < this.nowYear; i++) {
      this.yearList.unshift(i);
    }
    if (this.moreMonth.includes(this.nowMonth)) {
      month = 31;
    } else {
      //闰年二月
      if (this.nowMonth == 1 && this.nowMonth % 4 == 0 && this.nowMonth % 100 !== 0) {
        month = 29;
      }
      //平年二月
      if (this.nowMonth == 1 && (this.nowMonth % 4 !== 0 || this.nowMonth % 100 == 0)) {
        month = 28;
      }
      if (this.nowMonth !== 1) {
        month = 30;
      }
    }
    for (let i = 1; i < month + 1; i++) {
      this.currentDays.push({ status: 1, name: i });
    }
    let year = this.nowYear;
    for (let i = year - 10; i < year + 10; i++) {
      this.currentYears.push({ status: 1, name: i });
    }
    for (let i = 0; i < 12; i++) {
      this.currentMonths.push({ status: 1, name: i + 1 });
    }
  }
  //select day
  selectDay(item) {
    this.nowDay = item.name;
    // this.showType = null;
    // this.nowDate = new Date(this.nowYear,this.nowMonth,this.nowDay);
    // this.showMonth = this.nowMonth < 9 ? `0${this.nowMonth+1}`:this.nowMonth+1;
    let day = this.nowDay < 10 ? `0${this.nowDay}`:this.nowDay;
    this.selectDate = `${this.nowYear}-${this.showMonth}-${day} ${this.nowHour}:${this.nowMinute}`;
    // this.onModelChange(this.selectDate);
  }
  setDate() {
    this.showType = null;
    this.showMonth = this.nowMonth < 9 ? `0${this.nowMonth}`:this.nowMonth;
    let day = this.nowDay < 10 ? `0${this.nowDay}`:this.nowDay;
    this.selectDate = `${this.nowYear}-${this.showMonth}-${day} ${this.nowHour}:${this.nowMinute}`;
    this.onModelChange(this.selectDate);
  }
  selectYear(item, type = 'year') {
    if (type == 'month') {
      this.showType = 'day';
      this.nowMonth = item.name;
      this.isYear = true;
    } else {
      this.nowYear = item.name;
      this.isYear = false;
    }
    this.showMonth = this.nowMonth < 9 ? `0${this.nowMonth}` : this.nowMonth;
    let day = this.nowDay < 10 ? `0${this.nowDay}` : this.nowDay;
    this.selectDate = `${this.nowYear}-${this.showMonth}-${day} ${this.nowHour}:${this.nowMinute}`;

    this.onModelChange(this.selectDate);
  }
  //click input
  showDate(type = "LLL dd, yyyy") {
    if (type == "yyyy LLL") {
      this.showType = 'year';
    } else {
      this.showType = 'day';
    }
    if (this.selectDate) {
      this.handleDate();
    }
  }
  //next month or prev month
  changeMonth(type) {
    if (type > 0) {
      if (this.nowMonth == 11) {
        this.nowYear += 1;
        this.nowMonth = 0;
        // this.nowDate.setMonth(0);
      } else {
        this.nowMonth++;
        // this.nowDate.setMonth(this.nowMonth);
      }
    } else {
      if (this.nowMonth == 0) {
        this.nowYear -= 1;
        this.nowMonth = 11;
        // this.nowDate.setMonth(11);
      } else {
        this.nowMonth--;
        // this.nowDate.setMonth(this.nowMonth);
      }
    }
    this.nowDate = new Date(this.nowYear, this.nowMonth, this.nowDay);
    this.handleDate();
  }
  writeValue(currentValue: any) {
    if (!currentValue) {
      return;
    }
    this.nowYear = currentValue.substring(0, 4);
    this.nowMonth = Number(currentValue.substring(5, 7)) - 1;
    this.nowDay = currentValue.substring(8, 10);
    this.nowHour = currentValue.substring(11, 13);
    this.nowMinute = currentValue.substring(14, 16);
    this.showMonth = this.nowMonth < 9 ? `0${this.nowMonth + 1}` : this.nowMonth + 1;
    this.selectDate = `${this.nowYear}-${this.showMonth}-${this.nowDay} ${this.nowHour}:${this.nowMinute}`;
    this.handleDate();
  }
  registerOnChange(fn: Function): void {
    this.onModelChange = fn;
  }

  registerOnTouched(fn: Function): void {
    this.onModelTouched = fn;
  }
  changeHour(isAdd) {
    if (isAdd) {
      if (this.nowHour > 22) {
        this.nowHour = '00';
        return;
      }
      this.nowHour = Number(this.nowHour) + 1;
    } else {
      if (this.nowHour < 1) {
        this.nowHour = 23;
        return;
      }
      this.nowHour = Number(this.nowHour) - 1;
    }
    this.nowHour = this.nowHour < 10 ? `0${this.nowHour}` : this.nowHour
  }
  changeMinute(isAdd) {
    if (isAdd) {
      if (this.nowMinute > 58) {
        this.nowMinute = '00';
        return;
      }
      this.nowMinute = Number(this.nowMinute) + 1;

    } else {
      if (this.nowMinute < 1) {
        this.nowMinute = 59;
        return;
      }
      this.nowMinute = Number(this.nowMinute) - 1;
    }
    this.nowMinute = this.nowMinute < 10 ? `0${this.nowMinute}` : this.nowMinute
  }
}
